﻿
#include "circular_queue.h"

void InitQueue(CircularQueue* q) {

    q->front = 0;
    q->rear = 0;
}

bool IsEmpty(CircularQueue* q) {

    return q->front == q->rear;
}

bool IsFull(CircularQueue* q) {

    return (q->rear + 1) % MAX_SIZE == q->front;

}

bool Enqueue(CircularQueue* q, int value) {

    if (IsFull(q)) {
        printf("\nQueue is full.Cannot enqueue %d\n", value);
        return false;
    }

    q->data[q->rear] = value;
    q->rear = (q->rear + 1) % MAX_SIZE;
    return true;
}

bool DeQueue(CircularQueue* q,int * value) {

    if (IsEmpty(q)) {
        printf("Queue is empty. Cannot dequeue\n");
        return false;
    }
    *value = q->data[q->front];
    q->front = (q->front + 1) % MAX_SIZE;

    return true;
}

int GetHead(CircularQueue* q,int* value) {

    if (IsEmpty(q)) {
        printf("\nQueue is empty. No element.\n");
        return false;
    }

    *value = q->data[q->front];

    return true;
}

void TraverseQueue(CircularQueue* q) {

    if (IsEmpty(q)) {
        printf("\nQueue is empty. No element.\n");
        return ;
    }
    printf("Queue elements: ");

    int i = q->front;
    while(i != q->rear){
        printf("%d ",q->data[i]);

        i = (i + 1) % MAX_SIZE;
    }

    printf("\n");
}

void ClearQueue(CircularQueue* q) {
    q->front = 0;
    q->rear = 0;

    printf("\nQueue has been cleared.\n");
}



